package cn.iocoder.yudao.module.worker.controller.app.clerklevel;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.worker.controller.app.clerklevel.vo.*;
import cn.iocoder.yudao.module.worker.dal.dataobject.clerklevel.ClerkLevelDO;
import cn.iocoder.yudao.module.worker.service.clerklevel.ClerkLevelService;

@Tag(name = "用户 APP - 店员等级")
@RestController
@RequestMapping("/worker/clerk-level")
@Validated
public class AppClerkLevelController {

    @Resource
    private ClerkLevelService clerkLevelService;

    @PostMapping("/create")
    @Operation(summary = "创建店员等级")
    public CommonResult<Long> createClerkLevel(@Valid @RequestBody AppClerkLevelSaveReqVO createReqVO) {
        return success(clerkLevelService.createClerkLevel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新店员等级")
    public CommonResult<Boolean> updateClerkLevel(@Valid @RequestBody AppClerkLevelSaveReqVO updateReqVO) {
        clerkLevelService.updateClerkLevel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除店员等级")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteClerkLevel(@RequestParam("id") Long id) {
        clerkLevelService.deleteClerkLevel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得店员等级")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppClerkLevelRespVO> getClerkLevel(@RequestParam("id") Long id) {
        ClerkLevelDO clerkLevel = clerkLevelService.getClerkLevel(id);
        return success(BeanUtils.toBean(clerkLevel, AppClerkLevelRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得店员等级分页")
    public CommonResult<PageResult<AppClerkLevelRespVO>> getClerkLevelPage(@Valid AppClerkLevelPageReqVO pageReqVO) {
        PageResult<ClerkLevelDO> pageResult = clerkLevelService.getClerkLevelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppClerkLevelRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出店员等级 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportClerkLevelExcel(@Valid AppClerkLevelPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ClerkLevelDO> list = clerkLevelService.getClerkLevelPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "店员等级.xls", "数据", AppClerkLevelRespVO.class,
                        BeanUtils.toBean(list, AppClerkLevelRespVO.class));
    }

}